# encoding:utf-8
import time,asyncio,aiohttp
import csv,json

"""
营销低压线上缴费分析

对线下用户地址 转化成GPS坐标，便于只做地理分布图。

"

ak = "vZk1qCS2gnsDR0GaBD9hTBbTYYtO1UiG"


url_lst_failed =[]
url_lst_successed = []
lng_lat_lst = []
url = "http://api.map.baidu.com/geocoding/v3/?address={}&output=json&ak=vZk1qCS2gnsDR0GaBD9hTBbTYYtO1UiG"
async def get_gps(url,userid,addr, semaphore):
    async with semaphore:
        async with aiohttp.ClientSession() as session:
            async with session.get(url.format(addr), timeout=5) as resp:
                if resp.status != 200:
                    url_lst_failed.append(userid)
                else:
                    rest = await resp.text()
                    resj = json.loads(rest)
                    url_lst_successed.append(addr)
                    if resj["status"] == 0:
                        loc_info = resj["result"]["location"]
                        lng = loc_info["lng"]
                        lat = loc_info["lat"]
                        lng_lat_lst.append([userid,lng,lat])
                        if len(lng_lat_lst)%1000 == 0:
                            print("lng_lat_lst:",len(lng_lat_lst))
                            print("url_lst_failed:",len(url_lst_failed))
                    else:
                        print("status:",resj["status"])
                        url_lst_failed.append(userid)

with open("user_offline_addr.csv","r",encoding="utf8") as f:
    r = csv.reader(f)
    dat = [line for line in r]
    print(len(dat))

async def run():
    semaphore = asyncio.Semaphore(12) # 限制并发量为10
    tasks = [get_gps(url,d[0],d[1],semaphore) for d in dat] #总共1000任务
    await asyncio.wait(tasks)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())
    loop.close()
    print(len(lng_lat_lst))
    print(len(url_lst_failed))
    with open("user_geo_Baidu.csv",'a' ,newline="") as f:
        w = csv.writer(f)
        for d in lng_lat_lst:
            w.writerow(d)
    with open("user_geo_err.csv",'a' ,newline="") as f:
        w = csv.writer(f)
        for d in url_lst_failed:
            w.writerow(d)
